package com.dragonfly.shutter.services;

import java.util.Date;

import com.dragonfly.shutter.DAOs.AlbumDAO;
import com.dragonfly.shutter.DAOs.BrowseLogDAO;
import com.dragonfly.shutter.DAOs.FriendDAO;
import com.dragonfly.shutter.DAOs.PictureAlbumDAO;
import com.dragonfly.shutter.beans.Album;
import com.dragonfly.shutter.beans.BrowseLog;
import com.dragonfly.shutter.beans.PagedBean;

/**
 * 相册内图片列表服务类
 * @author 黄挺
 *
 */
public class AlbumPictureListService {
	private AlbumDAO albumDAO;
	private FriendDAO friendDAO;
	private BrowseLogDAO browseLogDAO;
	private PictureAlbumDAO pictureAlbumDAO;
	private static int pageSize = 10;
	
	/**
	 * 相册内图片列表服务函数
	 * @param userID 用户ID
	 * @param albumID 相册ID
	 * @param page 分页页码
	 * @param requestURL 请求URL
	 * @return 分页Bean
	 * @throws Exception 相册不存在或者权限错误
	 */
	public PagedBean doService(Integer userID, Integer albumID, Integer page, String requestURL) throws Exception {
		BrowseLog browseLog = null;
		
		if(userID == null)
			browseLog = new BrowseLog(new Date(System.currentTimeMillis()), requestURL);
		else
			browseLog = new BrowseLog(userID, new Date(System.currentTimeMillis()), requestURL);
		
		PagedBean pagedBean = new PagedBean();
		pagedBean.setPageNum(page);
		pagedBean.setPageSize(pageSize);
		return doDBOperate(userID, albumID, page, pagedBean, browseLog);
	}

	/**
	 * 相册图片列表数据库操作类
	 * @param userID 用户ID
	 * @param albumID 相册ID
	 * @param page 分页页码
	 * @param pagedBean 分页Bean
	 * @param browseLog 日志Bean
	 * @return 分页Bean
	 * @throws Exception 空的相册获取相册的访问权限不够
	 */
	private PagedBean doDBOperate(Integer userID, Integer albumID, Integer page,
			PagedBean pagedBean, BrowseLog browseLog) throws Exception {
		Album album = albumDAO.selectAlbumByID(albumID);
		
		//空的相册
		if(album == null){
			throw new Exception("找不到相册");
		}
		
		//访问权限检查
		if(album.getFlagPermission() == "private" && 
				(userID == null || userID != album.getOwner().getUserID())){
			throw new Exception("您不是这个相册的所有者，无权访问该相册");
		}
		
		if(album.getFlagPermission() == "friend" &&
				 ( userID != album.getOwner().getUserID()  || friendDAO.selectOneFriend(album.getOwner().getUserID(), userID) == null)){
			throw new Exception("您不是这个相册的所有者或者是所有者的好友，无权访问该相册");
		}
		
		int minRow = ( page - 1 ) * pageSize + 1;
		int maxRow = page * pageSize; 
		
		pagedBean.setPagedItem(pictureAlbumDAO.selectPagedPictureAlbum(albumID, minRow, maxRow));
		pagedBean.setItemNum(pictureAlbumDAO.selectPicCountByAlbumID(albumID));
		int maxPage = (int) java.lang.Math.ceil( (double)pagedBean.getItemNum() / (double)pageSize );
		pagedBean.setMaxPage(maxPage);
		
		browseLogDAO.insertBrowseLog(browseLog);
		
		return pagedBean;
	}

	public void setAlbumDAO(AlbumDAO albumDAO) {
		this.albumDAO = albumDAO;
	}

	public void setFriendDAO(FriendDAO friendDAO) {
		this.friendDAO = friendDAO;
	}

	public void setBrowseLogDAO(BrowseLogDAO browseLogDAO) {
		this.browseLogDAO = browseLogDAO;
	}

	public void setPictureAlbumDAO(PictureAlbumDAO pictureAlbumDAO) {
		this.pictureAlbumDAO = pictureAlbumDAO;
	}

}
